/**
 * @author David Young
 */
package csab.core.structs;
import java.awt.Rectangle;
import java.util.*;

import csab.core.Collidable;

/**
 * This class is utilized to provide a facade interface for the QuadTreeNode class.
 * @author David Young
 * @see QuadTreeNode
 */
public class QuadTree {
	private QuadTreeNode head;
	
	/**
	 * This is the constructor for the QuadTree.
	 * @param mapw Width of the area encompassed by the QuadTree.
	 * @param maph Height of the area encompassed by the QuadTree.
	 */
	public QuadTree(int mapw, int maph) {
		head = new QuadTreeNode(new Rectangle(0, 0, mapw, maph));
	}
	
	/**
	 * Adds a single item into the QuadTree.
	 * @param item The Collidable object to add into the tree.
	 * @return true if the item was added, false if it was not.
	 * This usually indicates that the item could not fit wholly 
	 * within the defined bounds of the tree.
	 * @see Collidable
	 */
	public boolean add(Collidable item) {
		if(head.getSection().contains(item.getCollisionBox()))
		{
			head.addItem(item);
			return true;
		}
		else
			return false;
	}
	
	/**
	 * Adds a sequence of items into the QuadTree structure.
	 * @param items The list of Collidable objects to add into
	 * the tree structure.
	 * @see Collidable
	 * @see List
	 */
	public void addAll(List<Collidable> items)
	{
		Iterator<Collidable> itr = items.iterator();
		while(itr.hasNext())
		{
			add(itr.next());
		}
	}
	
	/**
	 * Perform collisions against the Collidable item
	 * parameter.
	 * @param item The item to perform collisions against.
	 * @return true if item was able to collide against
	 * every potential object in the tree. If this is
	 * false, it likely means that something caused item
	 * to have its' location displaced.
	 * @see csab.core.Collidable
	 */
	public final boolean doCollisions(Collidable item)
	{
		return head.doCollisions(item);
	}
	
	/**
	 * Get the number of items contained within
	 * this tree.
	 * @return The number of items in this tree.
	 */
	public int getItemCount()
	{
		return head.getItemCount();
	}
	
	/**
	 * Return the current depth of the tree.
	 * @return An integer representing the
	 * tree's current depth (n, not n-1).
	 */
	public int getTreeDepth()
	{
		return head.getCurrentLevel();
	}
	
	/**
	 * Query the tree for a list of objects colliding against
	 * item.
	 * @param item The Collidable which is being tested for
	 * collisions.
	 * @return A List of Collidable objects that have collided
	 * with item.
	 * @see csab.core.Collidable
	 * @see java.util.List
	 */
	public List<Collidable> queryCollisions(Collidable item)
	{
		return head.queryCollisions(item);
	}
	
	/**
	 * Removes a sequence of items from the tree structure.
	 * @param items The items to remove from the tree.
	 * @return A list of removed items that were removed
	 * successfully.
	 * @see Collidable
	 * @see List
	 */
	public List<Collidable> removeAll(List<Collidable> items)
	{
		List<Collidable> removed = new LinkedList<Collidable>();
		return removed;
	}
	
	/**
	 * Removes an item from the tree structure.
	 * @param item The Collidable object to remove from the tree.
	 * @return true if the item could be removed, false if not.
	 * @see Collidable
	 */
	public boolean removeItem(Collidable item)
	{
		return head.removeItem(item);
	}
}
